package com.liangxy.zhyl.controller;

import com.liangxy.zhyl.base.PageResponse;
import com.liangxy.zhyl.base.ResponseResult;
import com.liangxy.zhyl.dto.UserDto;
import com.liangxy.zhyl.service.UserService;
import com.liangxy.zhyl.vo.UserVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户分页查询
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<UserVo>> page(@PathVariable("pageNum") int pageNum,
                                                     @PathVariable("pageSize") int pageSize,
                                                     @RequestBody UserDto userDto) {
        PageResponse<UserVo> page = userService.page(pageNum, pageSize,userDto);
        return ResponseResult.success(page);
    }

    /**
     * 添加用户
     * @param userDto
     * @return
     */
    @PutMapping
    public ResponseResult<Boolean> add(@RequestBody UserDto userDto) {
        Boolean flag = userService.add(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 修改用户
     * @param userDto
     * @return
     */
    @PatchMapping
    public ResponseResult<Boolean> update(@RequestBody UserDto userDto) {
        Boolean flag = userService.update(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("/current-user")
    public ResponseResult<UserVo> getCurrentUser() {
        UserVo userVo = userService.getCurrentUser();
        return ResponseResult.success(userVo);
    }

    /**
     * 启用-禁用用户
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult<Boolean> updateIsEnable(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        Boolean flag = userService.updateIsEnable(id, status);
        return ResponseResult.success(flag);
    }

    /**
     * 获取用户列表
     * @param userDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<UserVo>> list(@RequestBody UserDto userDto) {
        List<UserVo> userVoList = userService.list(userDto);
        return ResponseResult.success(userVoList);
    }

    @DeleteMapping("/remove/{userIds}")
    public ResponseResult<Boolean> remove(@PathVariable("userIds") Long userIds) {
        Boolean flag = userService.remove(userIds);
        return ResponseResult.success(flag);
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @PostMapping("reset-passwords/(userId)")
    public ResponseResult<Boolean> resetPasswords(@PathVariable("userId") Long userId) {
        Boolean flag = userService.resetPasswords(userId);
        return ResponseResult.success(flag);
    }
    
}
